package model;

import com.example.towerdefence.R;

import controller.GameBoard;
import model.enums.Towers;
import model.surface.Grid;
import model.surface.Map;
import model.surface.ScreenPoint;
import model.towers.ITower;
import model.towers.TowerSpriteSupplier;
import model.utils.CustomDialogMenuForTowerSelection;
import model.utils.CustomDialogMenuForTowerSelection.OnMyDialogResult;
import android.app.Activity;
import android.content.Context;

/**
 * Class which is handling the touch events from the screen.
 * 
 * @author EvilTeam
 * 
 */
public class OnTouch {
	private float x, y;
	private Context context;
	private CustomDialogMenuForTowerSelection towerSelectionDialogMenu;
	private int resultImageFromDialog;
	private static boolean isDrawnARange;

	public OnTouch(float x, float y, Context context) {
		setX(x);
		setY(y);
		setContext(context);
		towerSelectionDialogMenu = new CustomDialogMenuForTowerSelection(
				(Activity) getContext());
	}

	/**
	 * When the screen is touched the appropriate action for the touch event is
	 * set here.
	 * 
	 * @return
	 */
	public boolean onTouch() {

		boolean isTouched = false;
		if (!DataHolder.getTowers().isEmpty()) {
			for (ITower tower : DataHolder.getTowers()) {
				if (TowerSpriteSupplier.checkSellAndUpgradeIconsForTouch(
						this.getX(), this.getY(), tower)) {
					return false;
				}
			}
			for (ITower tower : DataHolder.getTowers()) {
				if (tower.checkIfTheTowerIsTouched(this.getX(), this.getY())) {
					isTouched = true;
					isDrawnARange = true;
				}
			}
		}

		for (int i = 0; i < Map.getPath().size() - 1; i++) {
			if (Map.getPath()
					.get(i)
					.isOnThePath(this.getX(), this.getY(),
							Map.getPath().get(i + 1)))
				return true;
		}

		if (DataHolder.getTowers().isEmpty() || !isTouched) {
			if (isDrawnARange) {
				isDrawnARange = false;
				return true;
			} else {
				showDialogMenuForTowerSelection(this.getX(), this.getY());
			}
		}
		return true;
	}

	/**
	 * Shows the dialog menu where the player can select a tower and buy it.
	 * 
	 * @param x
	 * @param y
	 */
	private void showDialogMenuForTowerSelection(final float x, final float y) {

		towerSelectionDialogMenu.setDialogResult(new OnMyDialogResult() {
			@Override
			public void finish(int result) {
				resultImageFromDialog = result;
				int towerCost = 0;
				// final ScreenPoint screenPoint = Grid.getTowerPoint(x, y);
				switch (resultImageFromDialog) {
				case 1:
					towerCost = Integer.parseInt(GameBoard.getRes().getString(
							R.string.tower1Cost));
					if (GameBoard.getCurrentPlayer().getMoney() >= towerCost) {
						ScreenPoint screenPoint = Grid.getTowerPoint(x, y);
						if (screenPoint != null)
							DataHolder.addTower(Factory.getTower(
									Towers.FIRE_TOWER, screenPoint.getxPoint(),
									screenPoint.getyPoint(),
									DataHolder.getEnemies()));
						decreaseMoneyWhenSetTower(towerCost);
					}
					break;
				case 2:
					towerCost = Integer.parseInt(GameBoard.getRes().getString(
							R.string.tower2Cost));
					if (GameBoard.getCurrentPlayer().getMoney() >= towerCost) {
						ScreenPoint screenPoint = Grid.getTowerPoint(x, y);
						if (screenPoint != null)
							DataHolder.addTower(Factory.getTower(
									Towers.LASER_TOWER,
									screenPoint.getxPoint(),
									screenPoint.getyPoint(),
									DataHolder.getEnemies()));
						decreaseMoneyWhenSetTower(towerCost);
					}
					break;
				case 3:
					towerCost = Integer.parseInt(GameBoard.getRes().getString(
							R.string.tower3Cost));
					if (GameBoard.getCurrentPlayer().getMoney() >= towerCost) {
						ScreenPoint screenPoint = Grid.getTowerPoint(x, y);
						if (screenPoint != null)
							DataHolder.addTower(Factory.getTower(
									Towers.FROST_TOWER,
									screenPoint.getxPoint(),
									screenPoint.getyPoint(),
									DataHolder.getEnemies()));
						decreaseMoneyWhenSetTower(towerCost);
					}
					break;
				}

			}
		});
		towerSelectionDialogMenu.show();
	}

	/**
	 * Decreases the money of the players when he buys a tower.
	 * 
	 * @param towerCost
	 */
	private void decreaseMoneyWhenSetTower(int towerCost) {
		GameBoard.getCurrentPlayer().setMoney(
				GameBoard.getCurrentPlayer().getMoney() - towerCost);
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
